package com.jelly.common.util.bean;

import com.jelly.common.util.AddressUtil;
import com.jelly.common.util.web.RequestContextHolderUtil;
import com.jelly.web.modules.monitor.log.domain.LoginLog;
import com.jelly.web.modules.monitor.log.service.ILoginLogService;
import eu.bitwalker.useragentutils.UserAgent;

import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-08-17 14:49
 * @功能描述: 新增操作日志记录工具
 **/
public class LogBeanUtil {

    public static void loginLogMsg(String loginAccount,String msg,String isState){
        ILoginLogService loginLogService = SpringUtil.getBean(ILoginLogService.class);
        UserAgent userAgent = UserAgent.parseUserAgentString(RequestContextHolderUtil.getRequest().getHeader("User-Agent"));
        // 获取客户端操作系统
        String system = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();

        LoginLog loginLog = new LoginLog();

        //登陆账号
        loginLog.setLoginAccount(loginAccount);
        //获取当前登录者IP
        String ip = RequestContextHolderUtil.getIPAddress();
        loginLog.setLoginIp(ip);
        //调用阿里获取IP地址详情
        loginLog.setLoginLocation(AddressUtil.getRealAddressByIP(ip));
        //设置登录状态
        loginLog.setIsState(isState);
        //登录提示
        loginLog.setMsg(msg);
        //客户端操作系统
        loginLog.setLoginSystem(system);
        //客户端浏览器
        loginLog.setLoginBrowser(browser);

        loginLogService.insterLoginLog(loginLog);
    }

}
